import React from "react";
import { CopyBtnPropsType } from "./PropsType";
import copy from "copy-to-clipboard";
import Toast from "../Toast";
import classnames from "classnames";
import "./index.less";
const CopyBtn: React.FC<CopyBtnPropsType> = (props) => {
  const {
    style,
    className,
    text,
    str,
    toastInfo,
    showToast,
    onClick = () => {},
    customBtn,
  } = props;
  const cls = classnames("m-react-copy-btn", className);

  const onPress = () => {
    const bool = copy(str);
    if (bool && showToast) {
      Toast.info(toastInfo);
    } else {
      Toast.info("出错了");
    }
    onClick();
  };

  return customBtn ? (
    React.cloneElement(customBtn, {
        onClick: onPress,
      })
  ) : (
    <span onClick={onPress} style={{ ...style }} className={cls}>
      {text}
    </span>
  );
};
CopyBtn.defaultProps = {
  prefixCls: "my-react-copy-btn",
  text: "复制",
  showToast: true,
  toastInfo: "复制成功",
  str: "",
};
export default CopyBtn;
